<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>路由 | 课程简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C02-RouterAndBlueprint/Blueprint.html" />
    
    
    <link rel="prev" href="../C02-RouterAndBlueprint/index.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.1"
        data-chapter-title="路由"
        data-filepath="C02-RouterAndBlueprint/Router.md"
        data-basepath=".."
        data-revision="Sat May 11 2019 20:06:35 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutFlask.html">
            
                
                    <a href="../AboutFlask.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Flask介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/HelloWorld.html">
            
                
                    <a href="../C01-CreateProject/HelloWorld.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        HelloWorld程序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C01-CreateProject/AboutParams.html">
            
                
                    <a href="../C01-CreateProject/AboutParams.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        参数说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/StartDevelopmentServer.html">
            
                
                    <a href="../C01-CreateProject/StartDevelopmentServer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        开发服务器运行方式
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-RouterAndBlueprint/index.html">
            
                
                    <a href="../C02-RouterAndBlueprint/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        路由与蓝图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="3.1" data-path="C02-RouterAndBlueprint/Router.html">
            
                
                    <a href="../C02-RouterAndBlueprint/Router.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        路由
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-RouterAndBlueprint/Blueprint.html">
            
                
                    <a href="../C02-RouterAndBlueprint/Blueprint.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        蓝图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C03-RequestAndResponse/HandleRequest.html">
            
                
                    <a href="../C03-RequestAndResponse/HandleRequest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        处理请求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C03-RequestAndResponse/HandleResponse.html">
            
                
                    <a href="../C03-RequestAndResponse/HandleResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        处理响应
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/CookieAndSession.html">
            
                
                    <a href="../C03-RequestAndResponse/CookieAndSession.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie与Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-HookAndContext/index.html">
            
                
                    <a href="../C04-HookAndContext/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        请求钩子与上下文
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C04-HookAndContext/AbortAndErrorhandler.html">
            
                
                    <a href="../C04-HookAndContext/AbortAndErrorhandler.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C04-HookAndContext/Hook.html">
            
                
                    <a href="../C04-HookAndContext/Hook.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        请求钩子
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="C04-HookAndContext/Context.html">
            
                
                    <a href="../C04-HookAndContext/Context.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        上下文
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Flask-RESTful/index.html">
            
                
                    <a href="../C05-Flask-RESTful/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        Flask-RESTful
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Flask-RESTful/QuickStart.html">
            
                
                    <a href="../C05-Flask-RESTful/QuickStart.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        起步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C05-Flask-RESTful/AboutView.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        关于视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C05-Flask-RESTful/AboutRequest.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutRequest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        关于请求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C05-Flask-RESTful/AboutResponse.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        关于响应
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x8DEF;&#x7531;">&#x8DEF;&#x7531;</h1>
<pre><code class="lang-python"><span class="hljs-decorator">@app.route(&quot;/itcast&quot;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">view_func</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello world&quot;</span>
</code></pre>
<h2 id="1-&#x67E5;&#x8BE2;&#x8DEF;&#x7531;&#x4FE1;&#x606F;">1 &#x67E5;&#x8BE2;&#x8DEF;&#x7531;&#x4FE1;&#x606F;</h2>
<ul>
<li><p>&#x547D;&#x4EE4;&#x884C;&#x65B9;&#x5F0F;</p>
<pre><code class="lang-shell">flask routes
</code></pre>
<pre><code class="lang-shell">Endpoint  Methods  Rule
--------  -------  -----------------------
index     GET      /
static    GET      /static/<path:filename>
</path:filename></code></pre>
</li>
<li><p>&#x5728;&#x7A0B;&#x5E8F;&#x4E2D;&#x83B7;&#x53D6;</p>
<p>&#x5728;&#x5E94;&#x7528;&#x4E2D;&#x7684;url_map&#x5C5E;&#x6027;&#x4E2D;&#x4FDD;&#x5B58;&#x7740;&#x6574;&#x4E2A;Flask&#x5E94;&#x7528;&#x7684;&#x8DEF;&#x7531;&#x6620;&#x5C04;&#x4FE1;&#x606F;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8BFB;&#x53D6;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x83B7;&#x53D6;&#x8DEF;&#x7531;&#x4FE1;&#x606F;</p>
<pre><code class="lang-python">print(app.url_map)
</code></pre>
<p>&#x5982;&#x679C;&#x60F3;&#x5728;&#x7A0B;&#x5E8F;&#x4E2D;&#x904D;&#x5386;&#x8DEF;&#x7531;&#x4FE1;&#x606F;&#xFF0C;&#x53EF;&#x4EE5;&#x91C7;&#x7528;&#x5982;&#x4E0B;&#x65B9;&#x5F0F;</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> rule <span class="hljs-keyword">in</span> app.url_map.iter_rules():
    print(<span class="hljs-string">&apos;name={} path={}&apos;</span>.format(rule.endpoint, rule.rule))
</code></pre>
</li>
</ul>
<h4 id="&#x9700;&#x6C42;">&#x9700;&#x6C42;</h4>
<p>&#x901A;&#x8FC7;&#x8BBF;&#x95EE;<code>/</code>&#x5730;&#x5740;&#xFF0C;&#x4EE5;json&#x7684;&#x65B9;&#x5F0F;&#x8FD4;&#x56DE;&#x5E94;&#x7528;&#x5185;&#x7684;&#x6240;&#x6709;&#x8DEF;&#x7531;&#x4FE1;&#x606F;</p>
<h4 id="&#x5B9E;&#x73B0;">&#x5B9E;&#x73B0;</h4>
<pre><code class="lang-python"><span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">route_map</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x4E3B;&#x89C6;&#x56FE;&#xFF0C;&#x8FD4;&#x56DE;&#x6240;&#x6709;&#x89C6;&#x56FE;&#x7F51;&#x5740;
    &quot;&quot;&quot;</span>
    rules_iterator = app.url_map.iter_rules()
    <span class="hljs-keyword">return</span> json.dumps({rule.endpoint: rule.rule <span class="hljs-keyword">for</span> rule <span class="hljs-keyword">in</span> rules_iterator})
</code></pre>
<h2 id="2-&#x6307;&#x5B9A;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;">2 &#x6307;&#x5B9A;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;</h2>
<p>&#x5728; Flask &#x4E2D;&#xFF0C;&#x5B9A;&#x4E49;&#x8DEF;&#x7531;&#x5176;&#x9ED8;&#x8BA4;&#x7684;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x4E3A;&#xFF1A;</p>
<ul>
<li>GET</li>
<li>OPTIONS(&#x81EA;&#x5E26;)</li>
<li>HEAD(&#x81EA;&#x5E26;)</li>
</ul>
<p>&#x5229;&#x7528;<code>methods</code>&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#x7684;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;</p>
<pre><code class="lang-python"><span class="hljs-decorator">@app.route(&quot;/itcast1&quot;, methods=[&quot;POST&quot;])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">view_func_1</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello world 1&quot;</span>

<span class="hljs-decorator">@app.route(&quot;/itcast2&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">view_func_2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello world 2&quot;</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C02-RouterAndBlueprint/index.html" class="navigation navigation-prev " aria-label="Previous page: 路由与蓝图"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C02-RouterAndBlueprint/Blueprint.html" class="navigation navigation-next " aria-label="Next page: 蓝图"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
